console.log('this is content js')
console.log('document', document)
console.log('location', location)
console.log('window', window)

//创建页面函数
function createPage() {
    const page = $('<div id="cj_move_page"></div>')
    const h3 = $('<h3 id="cj_move_h3">My Chrome Ext Content Page</h3>')
    const but1 = $('<button id="cj_but1">消息通知</button>')
    const butpop = $('<button id="butpop">消息通知pop</button>')
    page.append(h3)
    page.append(but1)
    page.append("<hr/>")
    page.append(butpop)
    $('body').append(page)

    $('#cj_but1').click(async (e) => {
        console.log('e', e, chrome)
        // 发送消息
        chrome.runtime.sendMessage({action: "fromContent"});

    })

    //拖拽
    drag(cj_move_h3)
}

createPage()

//拖拽
function drag(ele) {
    let oldX, oldY, newX, newY
    ele.onmousedown = function (e) {
        if (!cj_move_page.style.right && !cj_move_page.style.bottom) {
            cj_move_page.style.right = 0
            cj_move_page.style.bottom = 0
        }
        oldX = e.clientX
        oldY = e.clientY
        document.onmousemove = function (e) {
            newX = e.clientX
            newY = e.clientY
            cj_move_page.style.right = parseInt(cj_move_page.style.right) - newX + oldX + 'px'
            cj_move_page.style.bottom = parseInt(cj_move_page.style.bottom) - newY + oldY + 'px'
            oldX = newX
            oldY = newY
        }
        document.onmouseup = function () {
            document.onmousemove = null
            document.onmouseup = null
        }
    }
}

chrome.runtime.onConnect.addListener((res) => {
    console.log('contentjs中的 chrome.runtime.onConnect：', res)
    if (res.name === 'fromPopup2Content') {
        res.onMessage.addListener(mess => {
            console.log('contentjs中的 res.onMessage.addListener：', mess)
            res.postMessage('哈哈哈，我是contentjs')
        })
    }
})